package retry

import (
	"bgs/conf"
	sqlc "bgs/db/sqlc"
	"bgs/nsqp/util"
	"github.com/nsqio/go-nsq"
	"go.uber.org/zap"
)

// NsqRetryRunner publish nsq message producer
type NsqRetryRunner struct {
	slog     *zap.SugaredLogger
	producer *nsq.Producer
}

// NewNsqRetryRunner initialization
func NewNsqRetryRunner(
	nsqConf conf.NsqSection,
	slog *zap.SugaredLogger,
) (proxy *NsqRetryRunner, err error) {
	proxy = &NsqRetryRunner{
		slog: slog.With("module", "NsqRetryRunner"),
	}
	config := nsq.NewConfig()

	producer, err := nsq.NewProducer(nsqConf.NsqdAddress, config)
	if err != nil {
		return
	}
	proxy.producer = producer
	nsqLogger := util.NewNoopNSQLogger(slog)
	producer.SetLogger(
		nsqLogger,
		nsq.LogLevelError,
	)

	// Gracefully stop the consumer.
	// consumer.Stop()

	return
}

// PublishAddProfitReceiver 发送添加分账者消息
func (s *NsqRetryRunner) PublishNsqFail(ptr *sqlc.NsqFail) (err error) {
	err = s.producer.Publish(ptr.Topic, ptr.Message)
	return
}
